package com.tyjc.unique;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.tyjc.unique.bean.SSQTrendChart;
import com.tyjc.unique.net.StringResultBean;
import com.tyjc.unique.net.UrlConstants;
import com.tyjc.lottery.R;

import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import http.Common.CommandUtil;

/**
 * Created by miao on 16/6/20.
 */
@ContentView(R.layout.fg_ssqphaseskip)
public class PhaseSkipFragment extends _AnalaysesBaseFragment{


    @ViewInject(R.id.redball)
    GridView redBall;

    @ViewInject(R.id.blueball)
    GridView blueBall;

    @ViewInject(R.id.phase_current)
    TextView phase_current;

    private ArrayList<SSQTrendChart> arrayChartItems;


    @Override
    public void showData() {
        super.showData();
        requestChart();
    }

    public class GridAdapter extends BaseAdapter {


        private String[] arrs  = null;
        private int type;

        public GridAdapter(String[] arrs, int type){
            this.arrs = arrs;
            this.type = type;
        }

        @Override
        public int getCount() {
            if(arrs == null ){
                return  0;
            }
            return arrs.length;
        }

        @Override
        public String getItem(int position) {
            return arrs[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if(convertView == null){
                holder = new ViewHolder();
                convertView = View.inflate(getActivity(), R.layout.item_phaseskip,null);

                holder.ballNumTxt = (TextView) convertView.findViewById(R.id.ballNum);
                holder.skipNumTxt = (TextView) convertView.findViewById(R.id.skip_num);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.skipNumTxt.setText(arrs[position]);
            holder.ballNumTxt.setText(position+1+"");
            if(type == 1){
                holder.ballNumTxt.setBackgroundResource(R.drawable.ball_red);
                if(Integer.parseInt(arrs[position])>15){
                    holder.skipNumTxt.setTextColor(Color.RED);
                }
            }else{
                holder.ballNumTxt.setBackgroundResource(R.drawable.ball_blue);
                if(Integer.parseInt(arrs[position])>15){
                    holder.skipNumTxt.setTextColor(Color.BLUE);
                }
            }
            return convertView;
        }

        class ViewHolder{
            TextView ballNumTxt;
            TextView skipNumTxt;
        }

    }



    private void requestChart() {
        RequestParams params = new RequestParams(UrlConstants.TEST);
        params.addBodyParameter("lotteryType", "1001");
        params.addBodyParameter("page", "1");
        params.addBodyParameter("count", "1");
        httpPost(CommandUtil.TREND_CHART, params, false);

    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {

        if (resultBean.getStatus() == 0) {
            arrayChartItems = (ArrayList<SSQTrendChart>) JSON.parseArray(
                    resultBean.getData(), SSQTrendChart.class);


            SSQTrendChart chart = arrayChartItems.get(0);

            redBall.setAdapter(new GridAdapter(chart.hongMiss(),1));
            blueBall.setAdapter(new GridAdapter(chart.lanMiss(),2));
            phase_current.setText("当前投注期:第"+(Integer.parseInt(chart.getPhase())+1)+"期");
        }

    }

    @Override
    protected void onFailure(int requestCode) {

    }
}
